/** 
 * @file VoiceCall.c
 * @Synopsis  
 * @author JzeTech 
 * @version 0.1 beta
 * @date 1009-08-18
 */
#include "net.h"
#include "netVoiceCall.h"

extern struct devconfig *global_config;
static char voiceTicket[31] = {0};
//int voiceport = 4603;
char CompressedFormat[6] ={"G.711a"};

void AuthToVoiceCallServ(int fd, char* buff, int bufLen)
{
	char *buffer = buff, vTicket[31] = {0};
	int	len = bufLen, sockfd = fd;

    GetVoiceCallTicket(buffer, len, vTicket);
	//check ticket
	//end check
    SendVCallMsg(sockfd);

	return;
}

void GetVoiceCallTicket(char *buff, int bufLen, char *ticket)
{
    char *str = NULL, *buffer = buff, *vticket = ticket;
    mxml_node_t *root = NULL, *node = NULL;
    int len = bufLen;

    root = (mxml_node_t *)ParserXML(buffer, len, VSP_SEND_XML);
	if(NULL == root)
	{
		LOGERROR("GetVoiceCallTicket : ParserXml error\n");
		return;
	}
   	node = (mxml_node_t *)HaveNode(root, "Ticket");
	str = (char *)GetKeyValue(node, "Ticket");

	strncpy(vticket, str, strlen(str));
    return;
}

void SendVCallMsg(int fd)
{
    char *buffer = NULL, *pos = NULL;
    int sockfd = fd, len = 0;

    buffer = (char *)malloc(sizeof(char)*128);
	pos = buffer + sizeof(VSP_RECVHEADER);
    pos = memcpy(pos, XMLHEADER, strlen(XMLHEADER));
	pos += strlen(XMLHEADER);
    pos = memcpy(pos, START_MESSAGE, strlen(START_MESSAGE));
	pos += strlen(START_MESSAGE);
	sprintf(pos, "<Port>%d</Port>\n", global_config->hxhtConf.listenPort);
    pos += strlen(pos);
    pos = memcpy(pos, END_MESSAGE, strlen(END_MESSAGE));
	pos += strlen(END_MESSAGE);

	len = pos - buffer;
	VSP_RECVHEADER hdr = InitVSP_RECVHeader(len - sizeof(VSP_RECVHEADER), VOICE_CALL_AUTH_RES, 0, 0);
	memcpy(buffer, &hdr, sizeof(VSP_RECVHEADER));
    if(SendDataFunc(buffer, len, len, NULL, sockfd) == -1)
    {
        LOGERROR("SendAuthVoiceCallToClient : SendDataFunc error!\n");
    }

	free(buffer);
	return;
}

void CliToVoiceCallServ(int fd)
{
    int sockfd = fd;

#if 0
	if(!CheckVCallCliPermission(pbuffer, len))
    {
        LOGERROR("VoiceCall : Ticket was illegal!\n");
        return;
    }
#endif
	SendVoiceMsg(sockfd);

	return;
}

int CheckVCallCliPermission(char *buff, int bufLen)
{
    char *buffer = NULL;
    int len = 0;
    char cTicket[31] = {0};

    buffer = buff;
    len = bufLen;
    
    GetVoiceCallTicket(buffer, len, cTicket);
    if(!strcmp(voiceTicket, cTicket))
        return(0);
    return(1);
}

void SendVoiceMsg(int fd)
{
    int sockfd = fd, len = sizeof(VSP_RECVHEADER);
    char *buffer = (char *)malloc(sizeof(char) * 256);
    char *pos = buffer + len;

    pos = memcpy(pos, XMLHEADER, strlen(XMLHEADER));
    pos += strlen(XMLHEADER);
    pos = memcpy(pos, START_MESSAGE, strlen(START_MESSAGE));
	pos += strlen(START_MESSAGE);
	sprintf(pos, "<nChannels>%d</nChannels>\n",
			global_config->audio_param.channels);
	pos += strlen(pos);
	sprintf(pos, "<wBitsPerSample>%d</wBitsPerSample>\n",
			global_config->audio_param.bitpersample);
	pos += strlen(pos);
	sprintf(pos, "<nSamplesPerSec>%d</nSamplesPerSec>\n",
			(global_config->audio_param.samplepersec[0] | global_config->audio_param.samplepersec[1] << 8));
	pos += strlen(pos);
	sprintf(pos, "<CompressedFormat>%s</CompressedFormat>\n", 
			global_config->audio_param.compressfm);
	pos += strlen(pos);

    pos = memcpy(pos, END_MESSAGE, strlen(END_MESSAGE));
	pos += strlen(END_MESSAGE);
    len = pos - buffer;
    VSP_RECVHEADER hdr = InitVSP_RECVHeader(len-sizeof(VSP_RECVHEADER), 
			                                VOICE_CALL_CONNECT_RES, 0, 0);
    memcpy(buffer, &hdr, sizeof(hdr));
    if(SendDataFunc(buffer, len, len, NULL, sockfd) == -1)
    {
	    LOGERROR("SendDataFunc error!\n");
    }

	free(buffer);
	return;
}
